package com.javajun.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.entity.SysUser;
import com.javajun.entity.UserRole;
import com.javajun.repository.SysUserRepository;
import com.javajun.service.MenuService;
import com.javajun.service.SysUserService;

@Controller
public class IndexController {

    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private MenuService menuService;
    
    @RequestMapping(value="/login", method=RequestMethod.GET)
    public String login(ModelMap map) {
        return "login";
    }
    @RequestMapping(value="/welcome", method=RequestMethod.GET)
    public String welcome(ModelMap map) {
        return "welcome";
    }
    
    @RequestMapping(value="/index")
    public String index() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        User user = (User) auth.getPrincipal();
        SysUser sysUser = sysUserRepository.findByUsername(user.getUsername());
        UserRole role = new UserRole();
        List<UserRole> roles = sysUser.getRoles();
        for (UserRole userRole : roles) {
            role = userRole;
        }
        String roleCode = role.getCode();
        String dirUrl = "";
        if (roleCode.equals("ROLE_ADMIN")) {
            dirUrl = "/index1";
        } else if (roleCode.equals("ROLE_ENTERPRISEADMIN")) {
            dirUrl = "/index2";
        } else if (roleCode.equals("ROLE_CUSTOMER")) {
            dirUrl = "index3";
        } else {
            dirUrl = "";
        }
        return "redirect:"+dirUrl;
    }
    
    @RequestMapping(value="/index1")
    public String sysadmin() {
        return "index1";
    }
    
    @RequestMapping(value="/index2")
    public String sysenter() {
        return "index2";
    }
    
    @RequestMapping(value="/index3")
    public String syscus() {
        return "index3";
    }
    
    @RequestMapping(value="/kanban")
    public String kanban() {
        return "kanban";
    }
    
    @RequestMapping(value="/menu")
    @ResponseBody
    public String getmenu() {
        String menu = menuService.getMenuByRoleCode(getRoleCode());
        // System.out.println(menu);
        return menu;
    }
    
    public String getRoleCode() {
        SecurityContext ctx = SecurityContextHolder.getContext();
        Authentication auth = ctx.getAuthentication();
        User user = (User) auth.getPrincipal();
        SysUser sysUser = sysUserRepository.findByUsername(user.getUsername());
        String roleCode = "";
        for (UserRole userRole : sysUser.getRoles()) {
            roleCode = userRole.getCode();
        }
        return roleCode;
    }
}
